package com.zzl.study.nacos.config;

import com.zzl.study.nacos.NacosServiceMultiManager;
import com.zzl.study.nacos.condition.ConditionalOnNacosMultiDiscoveryEnabled;
import com.zzl.study.nacos.properties.NacosMultiDiscoveryProperties;
import com.zzl.study.nacos.registry.NacosAutoServiceMultiRegistration;
import com.zzl.study.nacos.registry.NacosMultiRegistration;
import com.zzl.study.nacos.registry.NacosMultiRegistrationCustomizer;
import com.zzl.study.nacos.registry.NacosServiceMultiRegistry;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * Nacos服务多注册中心自动注册自动装配类
 *
 * @author: zhangzl
 * @date: 2025/7/15 14:38
 * @version: 1.0
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnNacosMultiDiscoveryEnabled
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
@AutoConfigureAfter({AutoServiceRegistrationConfiguration.class, AutoServiceRegistrationAutoConfiguration.class, NacosMultiDiscoveryAutoConfiguration.class})
public class NacosServiceMultiRegistryAutoConfiguration {

    /**
     * 向Spring容器注入一个Nacos服务多注册中心注册类
     *
     * @param nacosServiceMultiManager      Nacos多注册中心服务管理器
     * @param nacosMultiDiscoveryProperties Nacos多注册中心属性配置类
     * @return Nacos服务多注册中心注册类
     */
    @Bean
    public NacosServiceMultiRegistry nacosServiceMultiRegistry(NacosServiceMultiManager nacosServiceMultiManager, NacosMultiDiscoveryProperties nacosMultiDiscoveryProperties) {
        return new NacosServiceMultiRegistry(nacosServiceMultiManager, nacosMultiDiscoveryProperties);
    }

    /**
     * 向Spring容器注入一个Nacos多注册中心注册信息
     *
     * @param registrationCustomizers       Nacos多注册中心注册类定制器
     * @param nacosMultiDiscoveryProperties Nacos多注册中心属性配置类
     * @param context                       Spring容器上下文
     * @return Nacos多注册中心注册信息
     */
    @Bean
    @ConditionalOnBean(AutoServiceRegistrationProperties.class)
    public NacosMultiRegistration nacosMultiRegistration(ObjectProvider<List<NacosMultiRegistrationCustomizer>> registrationCustomizers, NacosMultiDiscoveryProperties nacosMultiDiscoveryProperties, ApplicationContext context) {
        return new NacosMultiRegistration(registrationCustomizers.getIfAvailable(), nacosMultiDiscoveryProperties, context);
    }

    /**
     * 向Spring容器注入一个Nacos多注册中心自动注册类
     *
     * @param registry                          Nacos服务多注册中心注册类
     * @param autoServiceRegistrationProperties 自动注册属性配置
     * @param registration                      Nacos多注册中心注册信息
     * @return Nacos多注册中心自动注册类
     */
    @Bean
    @ConditionalOnBean(AutoServiceRegistrationProperties.class)
    public NacosAutoServiceMultiRegistration nacosAutoServiceMultiRegistration(NacosServiceMultiRegistry registry, AutoServiceRegistrationProperties autoServiceRegistrationProperties, NacosMultiRegistration registration) {
        return new NacosAutoServiceMultiRegistration(registry, autoServiceRegistrationProperties, registration);
    }

}
